package Deom1;

import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

public class Driver_Manager {
    public static void main(String[] args) {
        //加载数据库厂商提供的驱动
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取数据库连接
             connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/exam?characterEncoding=utf8" +
                    "&allowPublicKeyRetrieval=true&useSSL=false", "root", "123456");
            //创建statement对象
             statement=connection.createStatement();
            //定义sql并执行
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入学生姓名：");
            String name=scanner.next();
            String sql="select id,name,class_id from student where name='"+name+"'";
            //获取结果集
             resultSet=statement.executeQuery(sql);//执行查询
           // statement.executeUpdate(sql);//执行修改
while (resultSet.next()){
    int id=resultSet.getInt(1);
    String Name=resultSet.getString(2);
    int class_id=resultSet.getInt(3);
    System.out.println(MessageFormat.format("学生编号={0}，学生姓名={1},班级编号={3}",id,Name,class_id));
}
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
